package com.msmall.community.bbs.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.Constants;
import com.msmall.base.util.DateUtil;
import com.msmall.base.util.FileIO;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;


/**
 * 
 * @author zhzy
 *
 */
public final class BbsUtil {

	private static final Log logger = LogFactory.getLog(BbsUtil.class);
	
	public static final String PREFIX_WML = "file/wml/bbs/";
	public static final String PREFIX_HTML = "file/html/bbs/";
	
	/**
	 * BBS出错进入默认版区列表
	 */
	
	public static Long getBBSDefaultBoard(){
		try{
			return Long.valueOf(Configuration.getValue(Configuration.BBS_DEFAULT_BOARD_ID));
		}catch(Exception e){
			logger.error("===========>系统配置表没有BBS出错进入默认版区列表");
			return 1l;
		}
	}
	
	/**
	 * 获得帖子标题最少字符数
	 * @return
	 */
	
	public static int getTopicTitleMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_TOPIC_TITLE_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得帖子标题最多字符数
	 * @return
	 */
	public static int getTopicTitleMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_TOPIC_TITLE_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	/**
	 * 获得帖子内容最少字符数
	 * @return
	 */
	public static int getTopicContentMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_TOPIC_CONTENT_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得帖子内容最多字符数
	 * @return
	 */
	public static int getTopicContentMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_TOPIC_CONTENT_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * 判断帖子标题字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkTopicTitleLength(String title){
		if( StringUtil.nullOrBlank(title))
			return 2;
		int length = title.length();
		if(getTopicTitleMinChars() > 0 ){
			if(length < getTopicTitleMinChars())
				return 2;
		}
		if(getTopicTitleMaxChars() > 0 ){
			if( length > getTopicTitleMaxChars())
				return 3;
		}
		return 1;
	}
	/**
	 * 判断帖子内容字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkTopicContentLength(String content){
		if(StringUtil.nullOrBlank(content) )
			return 2;
		int length = content.length();
		if(getTopicContentMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getTopicContentMinChars())
				return 2;
		}
		if(getTopicContentMaxChars() > 0 ){
			if(length > getTopicContentMaxChars())
				return 3;
		}
		return 1;
	}
	
	/**
	 * 获得回帖内容最少字符数
	 * @return
	 */
	public static int getThreadContentMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_THREAD_CONTENT_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得回帖内容最多字符数
	 * @return
	 */
	public static int getThreadContentMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_THREAD_CONTENT_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * BBS帖子内容摘要长度
	 * @return
	 */
	public static int getContentAbstractChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.BBS_TOPIC_CONTENT_ABSTRACT_CHARS));
		}catch(Exception e){
			return 500;
		}
	}

	/**
	 * 判断回帖内容字符数是否在最大或最小之间
	 * @param title
	 * @return  2:字符数太少，3字符数太多，1正常
	 */
	public static int checkThreadContentLength(String content){
		if(StringUtil.nullOrBlank(content))
			return 2;
		int length = content.length();
		if(getThreadContentMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getThreadContentMinChars())
				return 2;
		}
		if(getThreadContentMaxChars() > 0 ){
			if(length > getThreadContentMaxChars())
				return 3;
		}
		return 1;
	}
	
	public static String readForumFile(String filePath){
		return FileIO.readFile(Constants.ROOTPATH + filePath, Constants.DEFAULT_CHARSET);
	}
	/**
	 * 写帖子文件 生成Topic和Thread存储路径 file/forum/bid/date/hours/minutes%10/wml/filename
	 * @param type 类型 0：Topic 1：Thread
	 * @return 0=filePath; 1=fileName; 2= fileExt(文件扩展名)
	 * @param title 标题
	 * @param content 内容
	 * @param entityId 实体ID，TopicID or ThreadID
	 * @param boardId 版区ID
	 * @param date 创建日期
	 * @throws IOException 
	 *
	 */
	public static String[] writeForumFile(String content, Long boardId, Date date, String uat) throws IOException{
		String filePath = getForumFolderPath(boardId, date); // bid/date/hours/minute%10/
		String fileName = getForumFileName();
		String path = Constants.ROOTPATH + getPrefixPath(uat) + filePath + fileName + Constants.FILE_HTML_EXTENDS;
		FileIO.writeFile(content, path);
		return new String[]{filePath, fileName, Constants.FILE_HTML_EXTENDS};
	}
	
	public static void updateForumFile(String content, String filePath, String fileName, String fileExt, String uat) throws IOException{
		FileIO.writeFile(content, Constants.ROOTPATH + getPrefixPath(uat) + filePath + fileName + fileExt);
	}
	
	public static void deleteForumFile(String filePath, String fileName, String uat){
		FileIO.deleteFile(Constants.ROOTPATH + getPrefixPath(uat)  + filePath + fileName);
	}
	
	public static String getForumFileName(){
		return FileIO.getRandomFileName();
	}
	/**
	 * 生成Topic和Thread存储路径 bid/date/hours/minute%10/
	 * @param boardId 版区ID
	 * @param date 创建时间
	 * @return bid/date/hours/minute%10/
	 */
	public static String getForumFolderPath(Long boardId, Date date){
		if(null == boardId || 0L == boardId || null == date){
			logger.error("create folderPath error!");
			return null;
		}
		String folderPath = "";
		StringBuilder builder = new StringBuilder();
		builder.append(boardId)
		.append(Constants.FILE_SEP)
		.append(DateUtil.formatDateTime(date, "yyyyMMdd"))
		.append(Constants.FILE_SEP)
		.append(DateUtil.getHour(date))
		.append(Constants.FILE_SEP)
		.append(DateUtil.getMinute(date)%10)
		.append(Constants.FILE_SEP);
		
		folderPath = builder.toString();
		File folder = new File(folderPath);
		if(!folder.exists())
			folder.mkdirs();
		return folderPath;
	}
	
	public static String getPrefixPath(String uat){
		if(Constants.USER_AGENT_TYPE_HTML.equals(uat)){
			return PREFIX_HTML;
		}
		if(Constants.USER_AGENT_TYPE_WML.equals(uat)){
			return PREFIX_WML;
		}
		return PREFIX_WML;
	}
}
